/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.beust.android.translate;

import com.beust.android.translate.Languages.Language;
import com.google.tts.TTS;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.Contacts;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import com.google.tts.TTS;

/**
 * Main activity for the Translate application.
 *
 * @author Krzysztof Roszkowski
 */
public class PreferencesActivity extends Activity {

	private Button mFromButton;
	private Button mToButton;
	private Button mOkButton;
	private Button mCancelButton;
	private RadioButton bModeT2TButton;
	private RadioButton bModeT2SButton;
	private RadioButton bModeS2SButton;
	private RadioButton bModeS2TButton;
	private CheckBox mPositionerOnOff;
	private Boolean bPositioner;
	private Button mLatestButton;
	private static final int LANGUAGEPREFERENCES_DIALOG_ID = 4;

	private TTS myTts;

	
	private OnClickListener mClickPreferencesListener = new OnClickListener() {
        public void onClick(View v) {
            mLatestButton = (Button) v;
            showDialog(LANGUAGEPREFERENCES_DIALOG_ID);
        }
    };
    
    private OnClickListener mPositionerOnOffListener = new OnClickListener() {
        public void onClick(View v) {
        	if(!mPositionerOnOff.isChecked())
        	{
        		mToButton.setVisibility(View.VISIBLE);
        		bPositioner = false;
        	}
        	else
        	{
        		mToButton.setVisibility(View.INVISIBLE);
        		bPositioner = true;
        	}
        }
    };
    
    private OnClickListener mOkListener = new OnClickListener() {
        public void onClick(View v) {
        	SavePreferences();
            finish();
        }
    };
    
    private OnClickListener mCancelListener = new OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    };
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);		
		
		setContentView(R.layout.preferences);
		
		SharedPreferences app_preferences = getSharedPreferences(TranslateActivity.PREF_FILE_NAME, MODE_PRIVATE);
		
		mOkButton = (Button) findViewById(R.id.ok);
		mCancelButton = (Button) findViewById(R.id.cancel);
		mFromButton = (Button) findViewById(R.id.fromPreferences); 
		mToButton = (Button) findViewById(R.id.toPreferences); 
		mPositionerOnOff = (CheckBox) findViewById(R.id.positioner);  
		bModeT2TButton = (RadioButton) findViewById(R.id.option1);
		bModeT2SButton = (RadioButton) findViewById(R.id.option2);
		bModeS2SButton = (RadioButton) findViewById(R.id.option3);
		bModeS2TButton = (RadioButton) findViewById(R.id.option4);
		
		boolean first_run = app_preferences.getBoolean("first_run_preferences", true);
        if (first_run) {
        	// set default language English for first run
        	Language from = Language.findLanguageByShortName(app_preferences.getString("from", "en"));
            updateButton(mFromButton, from);
            SharedPreferences.Editor editor = app_preferences.edit();
            editor.putBoolean("first_run_preferences", false);
            editor.commit();
            SetMode();
        }
        else
        {
        	updateButton(mFromButton, Language.findLanguageByShortName(app_preferences.getString("from", "en")));
        	updateButton(mToButton, Language.findLanguageByShortName(app_preferences.getString("to", "en")));
        	SetMode();
        
        }
		
		if(app_preferences.getBoolean("bPositioner", false))
		{
			mToButton.setVisibility(View.INVISIBLE);
			mPositionerOnOff.setChecked(true);
			bPositioner = true;
		}
		else
		{
			mPositionerOnOff.setChecked(false);
			bPositioner = false;
		}
		
		
        
		
		mPositionerOnOff.setOnClickListener(mPositionerOnOffListener);
		mToButton.setOnClickListener(mClickPreferencesListener);
		mFromButton.setOnClickListener(mClickPreferencesListener);
		mOkButton.setOnClickListener(mOkListener);
		mCancelButton.setOnClickListener(mCancelListener);

	}

	public String GetLanguage(){
		
		String f = ((Language) mFromButton.getTag()).getShortName();
		return f;
	}
	
	public Boolean GetPositioner(){
		
		Boolean result = false;// ((Language) mFromButton.getTag()).getShortName();
		return result;
	}

    private void updateButton(Button button, Language language) {
        language.configureButton(this, button);
    }
	
	 public void setNewLanguage(Language language, boolean from) {
		 updateButton(from ? mFromButton : mToButton, language);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog d) {
		if (id == LANGUAGEPREFERENCES_DIALOG_ID) {
	    boolean from = mLatestButton == mFromButton;
	    	((LanguageDialogPreferences) d).setFrom(from);
	    }
	}

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id == LANGUAGEPREFERENCES_DIALOG_ID) {
            return new LanguageDialogPreferences(this);
        }
        return null;
    }
    
    private void SavePreferences()
    {
    	SharedPreferences app_preferences = getSharedPreferences(TranslateActivity.PREF_FILE_NAME, MODE_PRIVATE);
    	SharedPreferences.Editor editor = app_preferences.edit();
    	editor.putBoolean("bPositioner", bPositioner);
        editor.putString("from", ((Language) mFromButton.getTag()).getShortName());
        if(!bPositioner)
        	editor.putString("to", ((Language) mToButton.getTag()).getShortName());
        editor.putString("mode", CheckMode());
        editor.commit();
    }
    
    private String CheckMode()
    {
    	if(bModeT2TButton.isChecked())
    		return "T2T";
    	if(bModeT2SButton.isChecked())
    		return "T2S";
    	if(bModeS2SButton.isChecked())
    		return "S2S";
    	if(bModeS2TButton.isChecked())
    		return "S2T";
    	
    	return "Error";
    }
    
    private void SetMode()
    {
    	SharedPreferences app_preferences = getSharedPreferences(TranslateActivity.PREF_FILE_NAME, MODE_PRIVATE);
    	String sMode = app_preferences.getString("mode", "T2T");
    		if(sMode.equals("T2T")) 
    		{	
    			bModeT2TButton.setChecked(true);
    			return;
    		}
    		if(sMode.equals("T2S")) 
    		{
    			bModeT2SButton.setChecked(true);
    			return;
    		}
    		if(sMode.equals("S2T")) 
    		{
    			bModeS2TButton.setChecked(true);
    			return;
    		}
    		if(sMode.equals("S2S")) 
    		{
    			bModeS2SButton.setChecked(true);
    			return;
    		}
    		
    	bModeT2TButton.setChecked(true);
    }
    
    
}
